package com.alibaba.citrus.cr.platform.enhance.repository.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.cr.common.util.SapAndCrmFieldDealUtils;
import com.alibaba.citrus.cr.platform.enhance.repository.ConsignmentOrderRepository;
import com.alibaba.citrus.cr.platform.enhance.repository.model.diff.ConsignmentOrder.ConsignmentOrderDTO;
import com.alibaba.citrus.cr.platform.enhance.repository.model.diff.ConsignmentOrder.ConsignmentOrderLineDTO;
import com.alibaba.lattice2.epoch.sdk.model.Result;

import com.epoch.app.crunifiedinventory.consignmentorder.dto.QueryConsignmentOrderByOutDeliveryOrderIdRequest;
import com.epoch.app.crunifiedinventory.consignmentorder.model.ConsignmentOrder;
import com.epoch.app.crunifiedinventory.consignmentorder.service.ConsignmentOrderService;
import com.epoch.app.crunifiedinventory.consignmentorderline.dto.QueryConsignmentOrderLineRequest;
import com.epoch.app.crunifiedinventory.consignmentorderline.model.ConsignmentOrderLine;
import com.epoch.app.crunifiedinventory.consignmentorderline.service.ConsignmentOrderLineService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

/**
 * @author: qiaojiange
 * @create: 2022/01/23
 * @Description 交货主单实现
 */
@Repository
public class ConsignmentOrderRepositoryImpl implements ConsignmentOrderRepository {

    @Resource
    ConsignmentOrderService consignmentOrderService;

    @Resource
    ConsignmentOrderLineService consignmentOrderLineService;


    @Override
    @RepositoryInvoker
    public Result<ConsignmentOrderDTO> queryBySap(String deliveryOrderId) {
        QueryConsignmentOrderByOutDeliveryOrderIdRequest request = new QueryConsignmentOrderByOutDeliveryOrderIdRequest();
        request.setOuterDeliveryOrderId(SapAndCrmFieldDealUtils.generateIdField(deliveryOrderId));
        Result<List<ConsignmentOrder>> listResult = consignmentOrderService.queryConsignmentOrderByOutDeliveryOrderId(request);

        if (Objects.nonNull(listResult) && CollectionUtils.isNotEmpty(listResult.getResult())) {
            ConsignmentOrder consignmentOrder = listResult.getResult().get(0);
            ConsignmentOrderDTO consignmentOrderDTO = new ConsignmentOrderDTO();
            BeanUtils.copyProperties(consignmentOrder, consignmentOrderDTO);
            return Result.success(consignmentOrderDTO);
        }
        return Result.fail("", "没有查询到交货单！");
    }

    /**
     * 查询交货子单
     *
     * @param consignmentOrderId
     * @return 交货子单
     */
    @Override
    @RepositoryInvoker
    public Result<List<ConsignmentOrderLineDTO>> queryConsignmentOrderLine(String consignmentOrderId) {
        QueryConsignmentOrderLineRequest request = new QueryConsignmentOrderLineRequest();
        request.setConsignmentOrderId(consignmentOrderId);
        Result<List<ConsignmentOrderLine>> listResult = consignmentOrderLineService.queryConsignmentOrderLine(request);
        if (Objects.nonNull(listResult) && CollectionUtils.isNotEmpty(listResult.getResult())) {
            List<ConsignmentOrderLineDTO> consignmentOrderLineDTOList = new ArrayList<>();
            for (ConsignmentOrderLine consignmentOrderLine : listResult.getResult()) {
                ConsignmentOrderLineDTO dto = new ConsignmentOrderLineDTO();
                BeanUtils.copyProperties(consignmentOrderLine, dto);
                consignmentOrderLineDTOList.add(dto);
            }

            if (CollectionUtils.isNotEmpty(consignmentOrderLineDTOList)) {
                return Result.success(consignmentOrderLineDTOList);
            }
        }
        return Result.fail("", "没有查询到交货子单！");
    }
}
